package com.example.service;

import com.example.dao.HealthycepingInfoDao;
import com.example.entity.*;
import com.example.entity.HealthycepingInfo;
import com.example.vo.HealthycepingInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class HealthycepingInfoService {

    @Resource
    private HealthycepingInfoDao healthycepingInfoDao;
	@Resource
    private SigninusersInfoService signinusersInfoService;
    
	
	//kuabiaojisuan3

    public HealthycepingInfo add(HealthycepingInfo healthycepingInfo) {
        //shangxchxuantupxian
        // 唯一校验
        
      // healthycepingInfo.setAccount((double) 0);
	  
        healthycepingInfoDao.insertSelective(healthycepingInfo);
        return healthycepingInfo;
    }
	
	public List<Map<String,String>> findByDiqu() {
        return healthycepingInfoDao.findByDiqu();
    }

    public void delete(Long id) {
        healthycepingInfoDao.deleteByPrimaryKey(id);
    }

    public void update(HealthycepingInfo healthycepingInfo) {
        //shangxchxuantupxian
		//youdianzan
        healthycepingInfoDao.updateByPrimaryKeySelective(healthycepingInfo);
    }

    public HealthycepingInfo findById(Long id) {
        return healthycepingInfoDao.selectByPrimaryKey(id);
    }
	
    public List<HealthycepingInfoVo> findAll() {
        return healthycepingInfoDao.findByCepingresults("all");
    }

    public PageInfo<HealthycepingInfoVo> findPage(String cepingresults, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
		
		List<HealthycepingInfoVo> all=null;
        Account user = (Account) request.getSession().getAttribute("user");
        if (user.getLevel().equals("管理员")) {all = healthycepingInfoDao.findByCepingresults(cepingresults);}
		if (user.getLevel().equals("注册用户")) {all = healthycepingInfoDao.findByCepingresultslist2(cepingresults,user.getUsername()); }
        
        
        return PageInfo.of(all);
    }
	
	public PageInfo<HealthycepingInfoVo> findPageqt(String cepingresults, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageHelper.startPage(pageNum, pageSize);
		
		List<HealthycepingInfoVo> all=null;
        Account user = (Account) request.getSession().getAttribute("user");
        all = healthycepingInfoDao.findByCepingresults(cepingresults);
		if (user.getLevel().equals("注册用户")) {all = healthycepingInfoDao.findByCepingresultslist2(cepingresults,user.getUsername()); }
        
	
	
        
        return PageInfo.of(all);
    }

   // public HealthycepingInfoVo findByUserName(String name) {
//        return healthycepingInfoDao.findByUsername(name);
//    }

	//yoxulogindenxglu
	
    public void changeStatus(Long id) {
        HealthycepingInfo healthycepingInfo = healthycepingInfoDao.selectByPrimaryKey(id);
        if(healthycepingInfo.getStatus().equals("是")){
            healthycepingInfo.setStatus("否");
            healthycepingInfoDao.updateByPrimaryKey(healthycepingInfo);
        }else if(healthycepingInfo.getStatus().equals("否")){
            healthycepingInfo.setStatus("是");
            healthycepingInfoDao.updateByPrimaryKey(healthycepingInfo);
        }
    }
	//ddaizdhifu
	//youtixing
	
	
	
	public List<HealthycepingInfo> getBysigninusersUsername(String username) {
    SigninusersInfo signinusersInfo = signinusersInfoService.findByUsername(username);
    return healthycepingInfoDao.findByusername(signinusersInfo);
    }
    
	
	//xiugaimimazys
	
}
